package socket;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

public class Client {
    private Socket socket;
    public Client(){
        try {
            System.out.println("正在请求连接..");

            socket = new Socket("localhost", 8088);
//            socket = new Socket("176.55.10.91", 8088);
            System.out.println("连接成功!");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public void start(){
        Scanner sc = new Scanner(System.in);
        String msg = "Hello server,i am client";
        int len = msg.getBytes(StandardCharsets.UTF_8).length;
        OutputStream os =null;
        try {
            os = socket.getOutputStream();
//            os.write(len);
//            os.write(msg.getBytes(StandardCharsets.UTF_8));
            byte[] date = "new byte[1024]".getBytes(StandardCharsets.UTF_8);
            os.write(date);
//            os.write(123);

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                socket.close();
                System.out.println("关闭连接");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        Client client = new Client();
        client.start();
    }
}
